package com.boot.rabbitmq.apidemo.model.routing;

import com.boot.rabbitmq.apidemo.model.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 *           路由模式
 */
public class Send {

    private final static String EXCHANGE_NAME = "test_exchange_direct";

    public static void sen(String message,String key) throws Exception {
        // 获取到连接以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();
        // 声明exchange
        channel.exchangeDeclare(EXCHANGE_NAME, "direct");
        // 消息内容  第二个参数  就是消息KEY
        channel.basicPublish(EXCHANGE_NAME, key, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
        channel.close();
        connection.close();
    }

    public static void main(String[] args) {
        for (int i=100;i>0;i--){
            try {
                sen("商品_"+i,"insert");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        for (int i=50;i>0;i--){
            try {
                sen("商品_"+i,"update");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
